#!/bin/sh

#
# Test the failure conditions of fat_touch
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
define(BOOT_PART_OFFSET, 63)
define(BOOT_PART_COUNT, 77238)

task bad_filename {
    on-init {
        fat_mkfs(\${BOOT_PART_OFFSET}, \${BOOT_PART_COUNT})
        fat_touch(\${BOOT_PART_OFFSET}, "star is bad **")
    }
}
task corrupt_partition {
    on-init {
        fat_mkfs(\${BOOT_PART_OFFSET}, \${BOOT_PART_COUNT})
        fat_mkdir(1, "foo")
    }
}
EOF

$FWUP_CREATE -c -f $CONFIG -o $FWFILE

echo "Expect to fail..."
if $FWUP_APPLY -q -d $IMGFILE -t bad_filename $FWFILE; then
    echo "Expected apply to fail due to a bad filename, but it didn't."
    exit 1
fi
echo "Expect to fail..."
if $FWUP_APPLY -q -d $IMGFILE -t corrupt_partition $FWFILE; then
    echo "Expected apply to fail due to a corrupt filesystem, but it didn't."
    exit 1
fi

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
